package com.example.mq.clients;

import com.example.mq.common.BinaryTool;
import com.example.mq.common.Request;
import com.example.mq.common.payloads.*;
import com.example.mq.server.virtualhost.ExchangeType;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class Producer1 {
    private Socket clientSocket;

    private InputStream inputStream;
    private OutputStream outputStream;
    private DataInputStream dataInputStream;
    private DataOutputStream dataOutputStream;

    private String clientTag = "producer1";

    public Producer1(String ip, int port) throws IOException {
        // 连接到服务器
        clientSocket = new Socket(ip, port);
    }

    public void start() {
        try {
            System.out.println("已连接到服务器");

            inputStream = clientSocket.getInputStream();
            outputStream = clientSocket.getOutputStream();
            dataInputStream = new DataInputStream(inputStream);
            dataOutputStream = new DataOutputStream(outputStream);

            // 构造请求
            PayloadQueueDeclare payloadQueueDeclare = new PayloadQueueDeclare();
            payloadQueueDeclare.setRid(generateRid());
            payloadQueueDeclare.setClientTag(clientTag);
            payloadQueueDeclare.setQueueName("queue1");
            byte[] payload1 = BinaryTool.toBytes(payloadQueueDeclare);
            Request request1 = new Request();
            request1.setType(0x3);
            request1.setLength(payload1.length);
            request1.setPayload(payload1);

            PayloadExchangeDeclare payloadExchangeDeclare = new PayloadExchangeDeclare();
            payloadExchangeDeclare.setRid(generateRid());
            payloadExchangeDeclare.setClientTag(clientTag);
            payloadExchangeDeclare.setExchangeName("exchange1");
            payloadExchangeDeclare.setExchangeType(ExchangeType.DIRECT);
            byte[] payload2 = BinaryTool.toBytes(payloadExchangeDeclare);
            Request request2 = new Request();
            request2.setType(0x1);
            request2.setLength(payload2.length);
            request2.setPayload(payload2);

            String bodyStr = "hello, world";
            byte[] body = BinaryTool.toBytes(bodyStr);
            PayloadProduce payloadProduce = new PayloadProduce();
            payloadProduce.setRid(generateRid());
            payloadProduce.setClientTag(clientTag);
            payloadProduce.setBody(body);
            payloadProduce.setExchangeName("exchange1");
            payloadProduce.setRoutingKey("queue1");
            byte[] payload3 = BinaryTool.toBytes(payloadProduce);
            Request request3 = new Request();
            request3.setType(0x7);
            request3.setLength(payload3.length);
            request3.setPayload(payload3);

            // 发送请求
            writeRequest(request1);
            writeRequest(request2);
            writeRequest(request3);


            // 接收响应
            // TODO


        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Client error: " + e.getMessage());
        }
    }

    private String generateRid() {
        return "R-" + UUID.randomUUID().toString();
    }

    public void stop() {
        // TODO
    }

    public void writeRequest(Request request) throws IOException {
        dataOutputStream.writeInt(request.getType());
        dataOutputStream.writeInt(request.getLength());
        dataOutputStream.write(request.getPayload());
        dataOutputStream.flush();
        System.out.println("[Connection] 发送请求! type=" + request.getType() + ", length=" + request.getLength());
    }
}
